# -*- coding: utf-8 -*-
# This code is opensource under WTFPL.
# Author: squaresum
import requests
import os
import time


def getContent(url):
    """
    请求各种文本内容
    """
    resp = requests.get(url)
    return resp.text


def getPicture(url, filename=None):
    """
    请求二进制内容并保存
    若无文件名则自动以URL最后一段进行命名
    """ 
    resp = requests.get(url)
    if not filename:
        filename = url.split('/')[-1]
    if isFileExist(filename):
        filename = '{}_{}'.format(str(int(time.time())), filename)
    with open(filename, 'wb') as f:
        f.write(resp.content)


def isFileExist(path):
    """
    确认文件是否存在，返回布尔值。
    """
    try:
        os.stat(path)
        return True
    except:
        return False
